package com.example.rabbit.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 简单模式
 * 消费者
 * 自动应答
 **/
public class Consumer {
    public static String queue_name = "mirror_hello";


    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("39.103.174.51");
        factory.setUsername("ifredom");
        factory.setPassword("654321");

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();


        DeliverCallback deliverCallback = (consumeTag, message) -> {
            System.out.println("成功" + new String(message.getBody()));
        };

        CancelCallback cancelCallback = (consumeTag) -> {
            System.out.println("取消回调" + consumeTag);
        };

        // 1. exchange 交换机名称，将消息发送到哪个交换机。为空字符串，使用默认的交换机
        // 2. autoAsk 消费成功后是否自动应答
        // 3. deliverCallback 消费者消费成功后的回调
        // 4. cancelCallback 消费者取消消费后回调
        channel.basicConsume(queue_name, true, deliverCallback, cancelCallback);
        System.out.println("消息接收完毕");
    }
}
